// Script created 10.29.09 12:35:07

var Tools = {
    coords : [],
    apply : function (data)
    {
         if (mouseDatum.id!='imagewindow') return false; 
         this.invoke (data);
         if (!Controller.tool) return false;
         if (! (this[Controller.tool] && this[Controller.tool].apply) ) return false;
         this[Controller.tool].apply (data);
         AttachLogWindow();
    },
    invoke : function (data)
    {
         if (mouseDatum.id!='imagewindow') return false; 
         if (!Controller.tool) return false;
         if (! (this[Controller.tool] && this[Controller.tool].invoke) ) return false;
         this[Controller.tool].invoke (data);
    },
    Draw : function (name) { 
        var pattern = '<td width="25%" align="center" bgcolor="(2)" onclick="Tools.Select(\'(1)\')"><img src="(0)"><br>(1)</td>';
        var color = Tools[name].invoke ? '#ffffff' : '#e0e0e0';
        return Tools[name].Icon ? pattern.format (Tools[name].Icon, name, name==Controller.tool?'#ffff00': color) : null;
    },
    Select : function (name) {
        if (!Tools[name].invoke) return alert ('(0) tool not initialized. Please try again later'.format(name));
        Controller.tool = name;
        ClearCursor();
        LoadBrush (function () {Controller.UpdateSidebar.invoke()});
    },
    Pencil : {
         Icon : '/pimp/images/pencil.gif',
         panel : ['Colorlist', 'Brushlist'],
         brush : true,
         color : true,
         work  : false,
         invoke : function (data)
         {  
             switch (data.name)
             {
                 case 'mouseup':
                     this.work = false;
                     break;
                 case 'mousedown':
                     this.work = true;
                     Tools.coords = [];
                     break;
                 case 'mousemove':
                     if (this.work)
                     {
                         Tools.coords.push ('(0)x(1)'.format(data.x,data.y));
                         DropBrush (data);
                         break;
                     }
             }
         },
         apply : function (data)
         {
             PleaseWait ();
             var f = webservice.ApplyToolAsync (Controller.picture.id, Controller.tool, Controller.brush, Controller.color, Tools.coords.join(' ')) 
           //  RefreshImage ();
         }
    },
    Wand : {
         Icon : '/pimp/images/wand.gif'
    },
    Lasso : {
         Icon : '/pimp/images/lasso.gif'
    },
    Zoom : {
         Icon : '/pimp/images/zoom.gif'
    },
    Dropper : {
         Icon : '/pimp/images/dropper.gif',
         work  : false,
         panel : ['Colorlist'],
         invoke : function (data)
         { 
             switch (data.name)
             {
                 case 'mouseup':
                     this.work = false;
                     break;
                 case 'mousedown':
                     this.work = true; 
                     Tools.coords = [data.x,data.y];
                     break; 
             }
         },
         apply : function (data)
         {
             var f = webservice.GetPixelAt (Controller.picture.id, Tools.coords[0], Tools.coords[1]) 
             SetColor (f)
         }
    },
    Eraser : {
         brush : true,
         Icon : '/pimp/images/eraser.gif',
         work  : false,
         panel : ['Brushlist'],
         invoke : function (data)
         { 
             switch (data.name)
             {
                 case 'mouseup':
                     this.work = false;
                     break;
                 case 'mousedown':
                     this.work = true;
                     Tools.coords = [];
                     break;
                 case 'mousemove':
                     if (this.work)
                     {
                         Tools.coords.push ('(0)x(1)'.format(data.x,data.y));
                         DropBrush (data,'FFFFFF');
                         break;
                     } 
             }
         },
         apply : function (data)
         {
             PleaseWait ();
             var f = webservice.ApplyToolAsync (Controller.picture.id, Controller.tool, Controller.brush, '', Tools.coords.join(' ')) 
            // RefreshImage ();
         }
    },
    Bucket : {
         color : true,
         Icon : '/pimp/images/bucket.gif',
         panel : ['Colorlist'],
         invoke : function (data)
         { 
             switch (data.name)
             {
                 case 'mouseup':
                     this.work = false;
                     break;
                 case 'mousedown':
                     this.work = true;
                     Tools.coords = [data.x,data.y];
                     break; 
             }
         },
         apply : function (data)
         {
             PleaseWait ();
             var f = webservice.ApplyTool (Controller.picture.id, Controller.tool, '', Controller.color, Tools.coords.join(' ')) 
             RefreshImage ();
         }
    },
Text 	: {
         color : true,
         Icon : '/pimp/images/text.gif'
    },	 
Crop 	: { 
         Icon : '/pimp/images/clip.gif'
    },	
Move 	: { 
         Icon : '/pimp/images/move.gif'
    },	
Filter 		: { 
         Icon : '/pimp/images/filter.gif'
    },					
Rotate 	: { 
         Icon : '/pimp/images/rotate.gif'
    },	
Scale 		: { 
         Icon : '/pimp/images/scale.gif'
    },
Rect 		: {
         color : true,
         Icon : '/pimp/images/rect.gif'
    },
Oval: {
         color : true,
         Icon : '/pimp/images/oval.gif'
    },

    Marquee : {
         object : null,
         work   : false,
         start  : {},
         apply : function (data)
         {
              if (this.object && this.object.load)
                  this.object.load (Tools.coords);
         },
         invoke : function (data)
         { 
             switch (data.name)
             {
                 case 'mouseup':
                     Tools.coords.push(data.x, data.y);
                     this.work = false;
                     break;
                 case 'mousedown':
                      MarqueeEngine.dispose();
                      this.object = MarqueeEngine.create (data);
                      if (!this.object) return alert ('Failed to create Marquee object')
                      Tools.coords = [data.x,data.y];
                      this.work = true;
                      break;
                 case 'mousemove':
                     if (this.work && this.object)
                     {
                         this.object.move (data)
                     } 
                     break;
             }
         } ,

         Icon : '/pimp/images/marquee.gif',
         Desc : 'Use your mouse to outline the area you wish to edit'
        
          
    }

}


function ClearBrush ()
{
    $('dbrush').innerHTML = '';

        var dw=$('dwait');
        dw.style.display = 'none';
}

function AttachBrush ()
{ 
   var d;
    if (!$('br'))
    {
         var d=document.createElement ('div');
         d.id='br'
         d.style.position = 'absolute'
         d.invoke = function (pos)
         { 
            this.style.display = '';
            var sz = { w:this.offsetWidth/2,h:this.offsetHeight/2 }
            this.style.left    = '(0)px'.format(pos.x - sz.w);
            this.style.top     = '(0)px'.format(pos.y - sz.h);
         }
         Controller.icursor.subscribe (d);
         $('imagewindow').appendChild (d);
    }
    d=$('br');
    var b=Controller.brush * Controller.info.z;
    d.innerHTML = '<img id="ir" onload="AttachBrushCursor()" src="working/mkbrush/(0)/(1)">'.format(b,Controller.color);
}

function AttachBrushCursor ()
{
    var d=$('ir');
    d.invoke = function ()
    {        
        var b=Controller.brush * Controller.info.z;
        this.src = 'working/mkbrush/(0)/(1)/hollow'.format(b,Controller.color);
    }
    d.invoke ();
  //  Controller.scroller.subscribe (d);
}

function LoadBrush (callback)
{
    var im=new Image();
    im.onload = callback
    im.onerror = callback;
    var b=Controller.brush * Controller.info.z;
    im.src = "working/mkbrush/(0)/(1)/hollow".format(b,Controller.color);
    AttachBrush ();
}

function DropBrush (pos,color)
{ 
    var d=$('dbrush');
    if (!d) return;
    var w=getPosition($('imagewindow'));
    var n=document.createElement ('div');
    var b=Controller.brush * Controller.info.z;
    n.style.position = 'absolute';
    n.innerHTML = '<img src="working/mkbrush/(0)/(1)">'.format(b,color||Controller.color);
    n.style.left = '(0)px'.format( ( pos.info.abs.x + w.x ) - ( b / 2)   )
    n.style.top = '(0)px'.format( ( pos.info.abs.y + w.y ) - ( b / 2) );
    d.appendChild (n);
}